--- 模块功能：利用http上传日志到指定服务器.
-- @author zhangFenghe
-- @module http.logUpload
-- @license MIT
-- @copyright zhangFenghe
-- @release 2022.04.29

module(..., package.seeall)

require "httpTask"
require "nvm"
require "io"
require "logGenerator"
nvm.init("config.lua", true)
local LOG_DIR_PATH = "/log_dir"

function uploadLogTask(tjsondata)
    local date, uploadVideoUrl, clientNo = tjsondata["date"], nvm.get("uploadVideoUrl"), nvm.get("info")[1]
    local fileName = clientNo .. "-" .. date
    local logName, errorName, eqlogName = fileName .. ".log", fileName .. ".error", fileName .. ".eqlog"
    local logPath, errorPath, eqlogPath = LOG_DIR_PATH .. "/" .. logName, LOG_DIR_PATH .. "/" .. errorName, LOG_DIR_PATH .. "/" .. eqlogName
    log.info("日志名称", logName, "日志路径", logPath)
    if io.exists(logPath) then
        httpTask.postMultipartFormData(
            uploadVideoUrl,
            nil,
            {
                files = {
                    [logName] = logPath
                }
            },
            {
                log = "multipart/form-data",
            },
            60000
        )
    end
    --[[ log.info("错误日志是否存在 ", io.exists(errorPath))
    if io.exists(errorPath) then
        httpTask.postMultipartFormData(
            uploadVideoUrl,
            nil,
            {
                files = {
                    [errorName] = errorPath
                }
            },
            {
                error = "multipart/form-data",
            },
            60000
        )
        log.info("错误日志名称", errorName, "错误日志路径", errorPath)
    end ]]
    log.info("eq日志文件是否存在 ", io.exists(eqlogPath))
    if io.exists(eqlogPath) then
        httpTask.postMultipartFormData(
            uploadVideoUrl,
            nil,
            {
                files = {
                    [eqlogName] = eqlogPath
                }
            },
            {
                eqlog = "multipart/form-data",
            },
            60000
        )
        log.info("eq日志名称", eqlogName, "eq日志路径", eqlogPath)
    end
end
